﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_QuanLyMaSoThue_List
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            BindToGrid()
        End If
    End Sub
    Private Sub BindToGrid(Optional ByVal iPage As Integer = 1 _
                            , Optional ByVal strMaCQT As String = "" _
                            , Optional ByVal strTenCQT As String = "" _
                            , Optional ByVal strSoNguoiNopThue As String = "" _
                            , Optional ByVal strMaSoThue As String = "" _
                            , Optional ByVal strTenNguoiNopThue As String = "" _
                            , Optional ByVal strMaNganhChinh As String = "" _
                            , Optional ByVal strTenNganhChinh As String = "" _
                            , Optional ByVal strSoNha As String = "" _
                            , Optional ByVal strMaPhuongXa As String = "" _
                            , Optional ByVal strTenPhuongXa As String = "" _
                            , Optional ByVal strMaHuyen As String = "" _
                            , Optional ByVal strHuyen As String = "" _
                            , Optional ByVal strMaTinh As String = "" _
                            , Optional ByVal strTenTinh As String = "" _
                            , Optional ByVal strTongSoLaoDong As String = "" _
                            , Optional ByVal strNganhNghe As String = "" _
                            , Optional ByVal strTenGiamDoc As String = "" _
                            , Optional ByVal strDienThoaiGiamDoc As String = "" _
                            , Optional ByVal strSoGiayTo As String = "" _
                            , Optional ByVal strNgayCap As String = "" _
                            , Optional ByVal strLoaiGiayTo As String = "" _
                            , Optional ByVal strSoGCN As String = "" _
                            , Optional ByVal strNgayCapGCN As String = "" _
                            , Optional ByVal strCMND As String = "" _
                            , Optional ByVal strTenChuong As String = "" _
                            , Optional ByVal strTenKhoan As String = "" _
                            , Optional ByVal strLoaiNguoiNopThue As String = "" _
                            , Optional ByVal strMaTrangThaiDKTCaNhan As String = "" _
                            , Optional ByVal strTrangThaiDKTCaNhan As String = "" _
                            , Optional ByVal strMaTrangThaiDKTToChuc As String = "" _
                            , Optional ByVal strTrangThaiDKTToChuc As String = "" _
                            , Optional ByVal strNgayNhanToKhai As String = "" _
                            , Optional ByVal strLoaiHinhKinhTe As String = "" _
                            , Optional ByVal strTamNghiTuNgay As String = "" _
                            , Optional ByVal strTamNghiDenNgay As String = "" _
                            , Optional ByVal strLyDoTamNghi As String = "" _
                            , Optional ByVal strLyDoDongMSTCaNhan As String = "" _
                            , Optional ByVal strNgayDongTrangThaiCaNhan As String = "" _
                            , Optional ByVal strLyDoDongMSTToChuc As String = "" _
                            , Optional ByVal strNgayDongTrangThaiToChuc As String = "" _
                            , Optional ByVal strQuocTich As String = "" _
                            , Optional ByVal strNgaySinh As String = "" _
                            , Optional ByVal strVonKinhDoanh As String = "" _
                            , Optional ByVal strNguonVonNSNN As String = "" _
                            , Optional ByVal strNguonVonNuocNgoai As String = "" _
                            , Optional ByVal strNguonVonKhac As String = "" _
                            , Optional ByVal strNgayBatDauKinhDoanh As String = "" _
                            , Optional ByVal strDienThoaiBan As String = "" _
            )
        Using data As New VSEntities
            If Session("HuyenId") <> 0 Then
                strMaHuyen = Session("HuyenId")
            Else
                strMaHuyen = "Admin"
            End If

            Dim arrSearch() As String = {iPage.ToString, strMaCQT.ToString, strTenCQT.ToString, strSoNguoiNopThue.ToString, strMaSoThue.ToString, strTenNguoiNopThue.ToString, strMaNganhChinh.ToString, strTenNganhChinh.ToString, strSoNha.ToString, strMaPhuongXa.ToString, strTenPhuongXa.ToString, strMaHuyen.ToString, strHuyen.ToString, strMaTinh.ToString, strTenTinh.ToString, strTongSoLaoDong.ToString, strNganhNghe.ToString, strTenGiamDoc.ToString, strDienThoaiGiamDoc.ToString, strSoGiayTo.ToString, strNgayCap.ToString, strLoaiGiayTo.ToString, strSoGCN.ToString, strNgayCapGCN.ToString, strCMND.ToString, strTenChuong.ToString, strTenKhoan.ToString, strLoaiNguoiNopThue.ToString, strMaTrangThaiDKTCaNhan.ToString, strTrangThaiDKTCaNhan.ToString, strMaTrangThaiDKTToChuc.ToString, strTrangThaiDKTToChuc.ToString, strNgayNhanToKhai.ToString, strLoaiHinhKinhTe.ToString, strTamNghiTuNgay.ToString, strTamNghiDenNgay.ToString, strLyDoTamNghi.ToString, strLyDoDongMSTCaNhan.ToString, strNgayDongTrangThaiCaNhan.ToString, strLyDoDongMSTToChuc.ToString, strNgayDongTrangThaiToChuc.ToString, strQuocTich.ToString, strNgaySinh.ToString, strVonKinhDoanh.ToString, strNguonVonNSNN.ToString, strNguonVonNuocNgoai.ToString, strNguonVonKhac.ToString, strNgayBatDauKinhDoanh.ToString, strDienThoaiBan.ToString}
            ViewState("search") = arrSearch

            'So ban ghi muon the hien tren trang
            Dim intPag_Size As Int32 = drpPage_Size.SelectedValue
            Dim p As List(Of uspNguoiNopThueSelectAll_Result) = data.uspNguoiNopThueSelectAll(strMaCQT _
                            , strTenCQT _
                            , strSoNguoiNopThue _
                            , strMaSoThue _
                            , strTenNguoiNopThue _
                            , strMaNganhChinh _
                            , strTenNganhChinh _
                            , strSoNha _
                            , strMaPhuongXa _
                            , strTenPhuongXa _
                            , strMaHuyen _
                            , strHuyen _
                            , strMaTinh _
                            , strTenTinh _
                            , strTongSoLaoDong _
                            , strNganhNghe _
                            , strTenGiamDoc _
                            , strDienThoaiGiamDoc _
                            , strSoGiayTo _
                            , strNgayCap _
                            , strLoaiGiayTo _
                            , strSoGCN _
                            , strNgayCapGCN _
                            , strCMND _
                            , strTenChuong _
                            , strTenKhoan _
                            , strLoaiNguoiNopThue _
                            , strMaTrangThaiDKTCaNhan _
                            , strTrangThaiDKTCaNhan _
                            , strMaTrangThaiDKTToChuc _
                            , strTrangThaiDKTToChuc _
                            , strNgayNhanToKhai _
                            , strLoaiHinhKinhTe _
                            , strTamNghiTuNgay _
                            , strTamNghiDenNgay _
                            , strLyDoTamNghi _
                            , strLyDoDongMSTCaNhan _
                            , strNgayDongTrangThaiCaNhan _
                            , strLyDoDongMSTToChuc _
                            , strNgayDongTrangThaiToChuc _
                            , strQuocTich _
                            , strNgaySinh _
                            , strVonKinhDoanh _
                            , strNguonVonNSNN _
                            , strNguonVonNuocNgoai _
                            , strNguonVonKhac _
                            , strNgayBatDauKinhDoanh _
                            , strDienThoaiBan _
            , iPage, intPag_Size).ToList
            Dim strKey_Name() As String = {"Id", "MaCQT", "MaSoThue"}
            'Tong so ban ghi
            If p.Count > 0 Then
                hidCount.Value = p.FirstOrDefault.Total()
                Create_Pager(hidCount.Value, iPage, intPag_Size, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
            If (hidCount.Value > 0) Then
                lblTotal.Text = "Hiển thị " + (((iPage - 1) * intPag_Size) + 1).ToString + " đến " + (((iPage - 1) * intPag_Size) + grdShow.Rows.Count).ToString + " trong tổng số " + hidCount.Value.ToString + " bản ghi."
            Else
                lblTotal.Text = ""
            End If
        End Using
    End Sub
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        Dim arrSearch() As String
        arrSearch = ViewState("search")
        BindToGrid(CInt(lnkTile.ToolTip) _
                , arrSearch(1) _
                , arrSearch(2) _
                , arrSearch(3) _
                , arrSearch(4) _
                , arrSearch(5) _
                , arrSearch(6) _
                , arrSearch(7) _
                , arrSearch(8) _
                , arrSearch(9) _
                , arrSearch(10) _
                , arrSearch(11) _
                , arrSearch(12) _
                , arrSearch(13) _
                , arrSearch(14) _
                , arrSearch(15) _
                , arrSearch(16) _
                , arrSearch(17) _
                , arrSearch(18) _
                , arrSearch(19) _
                , arrSearch(20) _
                , arrSearch(21) _
                , arrSearch(22) _
                , arrSearch(23) _
                , arrSearch(24) _
                , arrSearch(25) _
                , arrSearch(26) _
                , arrSearch(27) _
                , arrSearch(28) _
                , arrSearch(29) _
                , arrSearch(30) _
                , arrSearch(31) _
                , arrSearch(32) _
                , arrSearch(33) _
                , arrSearch(34) _
                , arrSearch(35) _
                , arrSearch(36) _
                , arrSearch(37) _
                , arrSearch(38) _
                , arrSearch(39) _
                , arrSearch(40) _
                , arrSearch(41) _
                , arrSearch(42) _
                , arrSearch(43) _
                , arrSearch(44) _
                , arrSearch(45) _
                , arrSearch(46) _
                , arrSearch(47) _
                , arrSearch(48) _
                 )
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub
#End Region
#Region "Event for control"
    Protected Sub lnkbtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim intId As Integer
        Dim strLogName As String = ""
        Using data As New VSEntities
            intId = grdShow.DataKeys(hidID.Value)("Id").ToString
            Dim q = (From p In data.NguoiNopThues Where p.Id = intId Select p).FirstOrDefault
            Try
                data.NguoiNopThues.DeleteObject(q)
                data.SaveChanges()
                'Insert_App_Log("Insert  NguoiNopThue:" & txtTitle.Text.Trim & "", Function_Name.NguoiNopThue, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Xóa dữ liệu thành công.');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
            End Try
        End Using
        BindToGrid()
    End Sub
    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt(drpPage_Size.SelectedValue) * (CInt(hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            Dim hplEdit As HyperLink = CType(e.Row.FindControl("hplEdit"), HyperLink)
            hplEdit.NavigateUrl = "../../Page/QuanLyMaSoThue/Edit.aspx?Id=" & grdShow.DataKeys(e.Row.RowIndex)("Id").ToString
            Dim hplMST As HyperLink = CType(e.Row.FindControl("hplMST"), HyperLink)
            hplMST.NavigateUrl = "../../Page/QuanLyMaSoThue/Detail.aspx?Id=" & grdShow.DataKeys(e.Row.RowIndex)("Id").ToString
            hplMST.Text = grdShow.DataKeys(e.Row.RowIndex)("MaSoThue").ToString
        End If
    End Sub
#End Region
#Region "Search"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        BindToGrid(1 _
                        , txtMacqt.Text.Trim _
                        , txtTencqt.Text.Trim _
                        , txtSonguoinopthue.Text.Trim _
                        , txtMasothue.Text.Trim _
                        , txtTennguoinopthue.Text.Trim _
                        , txtManganhchinh.Text.Trim _
                        , txtTennganhchinh.Text.Trim _
                        , txtSonha.Text.Trim _
                        , txtMaphuongxa.Text.Trim _
                        , txtTenphuongxa.Text.Trim _
                        , txtMahuyen.Text.Trim _
                        , txtHuyen.Text.Trim _
                        , txtMatinh.Text.Trim _
                        , txtTentinh.Text.Trim _
                        , txtTongsolaodong.Text.Trim _
                        , txtNganhnghe.Text.Trim _
                        , txtTengiamdoc.Text.Trim _
                        , txtDienthoaigiamdoc.Text.Trim _
                        , txtSogiayto.Text.Trim _
                        , txtNgaycap.Text.Trim _
                        , txtLoaigiayto.Text.Trim _
                        , txtSogcn.Text.Trim _
                        , txtNgaycapgcn.Text.Trim _
                        , txtCmnd.Text.Trim _
                        , txtTenchuong.Text.Trim _
                        , txtTenkhoan.Text.Trim _
                        , txtLoainguoinopthue.Text.Trim _
                        , txtMatrangthaidktcanhan.Text.Trim _
                        , txtTrangthaidktcanhan.Text.Trim _
                        , txtMatrangthaidkttochuc.Text.Trim _
                        , txtTrangthaidkttochuc.Text.Trim _
                        , txtNgaynhantokhai.Text.Trim _
                        , txtLoaihinhkinhte.Text.Trim _
                        , txtTamnghitungay.Text.Trim _
                        , txtTamnghidenngay.Text.Trim _
                        , txtLydotamnghi.Text.Trim _
                        , txtLydodongmstcanhan.Text.Trim _
                        , txtNgaydongtrangthaicanhan.Text.Trim _
                        , txtLydodongmsttochuc.Text.Trim _
                        , txtNgaydongtrangthaitochuc.Text.Trim _
                        , txtQuoctich.Text.Trim _
                        , txtNgaysinh.Text.Trim _
                        , txtVonkinhdoanh.Text.Trim _
                        , txtNguonvonnsnn.Text.Trim _
                        , txtNguonvonnuocngoai.Text.Trim _
                        , txtNguonvonkhac.Text.Trim _
                        , txtNgaybatdaukinhdoanh.Text.Trim _
                        , txtDienthoaiban.Text.Trim _
                        )
    End Sub
    Protected Sub drpPage_Size_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpPage_Size.SelectedIndexChanged
        Dim arrSearch() As String
        arrSearch = ViewState("search")
        BindToGrid(1, arrSearch(1).ToString _
                    , arrSearch(2).ToString _
                    , arrSearch(3).ToString _
                    , arrSearch(4).ToString _
                    , arrSearch(5).ToString _
                    , arrSearch(6).ToString _
                    , arrSearch(7).ToString _
                    , arrSearch(8).ToString _
                    , arrSearch(9).ToString _
                    , arrSearch(10).ToString _
                    , arrSearch(11).ToString _
                    , arrSearch(12).ToString _
                    , arrSearch(13).ToString _
                    , arrSearch(14).ToString _
                    , arrSearch(15).ToString _
                    , arrSearch(16).ToString _
                    , arrSearch(17).ToString _
                    , arrSearch(18).ToString _
                    , arrSearch(19).ToString _
                    , arrSearch(20).ToString _
                    , arrSearch(21).ToString _
                    , arrSearch(22).ToString _
                    , arrSearch(23).ToString _
                    , arrSearch(24).ToString _
                    , arrSearch(25).ToString _
                    , arrSearch(26).ToString _
                    , arrSearch(27).ToString _
                    , arrSearch(28).ToString _
                    , arrSearch(29).ToString _
                    , arrSearch(30).ToString _
                    , arrSearch(31).ToString _
                    , arrSearch(32).ToString _
                    , arrSearch(33).ToString _
                    , arrSearch(34).ToString _
                    , arrSearch(35).ToString _
                    , arrSearch(36).ToString _
                    , arrSearch(37).ToString _
                    , arrSearch(38).ToString _
                    , arrSearch(39).ToString _
                    , arrSearch(40).ToString _
                    , arrSearch(41).ToString _
                    , arrSearch(42).ToString _
                    , arrSearch(43).ToString _
                    , arrSearch(44).ToString _
                    , arrSearch(45).ToString _
                    , arrSearch(46).ToString _
                    , arrSearch(47).ToString _
                    , arrSearch(48).ToString _
                     )
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        txtMacqt.Text = ""
        txtTencqt.Text = ""
        txtSonguoinopthue.Text = ""
        txtMasothue.Text = ""
        txtTennguoinopthue.Text = ""
        txtManganhchinh.Text = ""
        txtTennganhchinh.Text = ""
        txtSonha.Text = ""
        txtMaphuongxa.Text = ""
        txtTenphuongxa.Text = ""
        txtMahuyen.Text = ""
        txtHuyen.Text = ""
        txtMatinh.Text = ""
        txtTentinh.Text = ""
        txtTongsolaodong.Text = ""
        txtNganhnghe.Text = ""
        txtTengiamdoc.Text = ""
        txtDienthoaigiamdoc.Text = ""
        txtSogiayto.Text = ""
        txtNgaycap.Text = ""
        txtLoaigiayto.Text = ""
        txtSogcn.Text = ""
        txtNgaycapgcn.Text = ""
        txtCmnd.Text = ""
        txtTenchuong.Text = ""
        txtTenkhoan.Text = ""
        txtLoainguoinopthue.Text = ""
        txtMatrangthaidktcanhan.Text = ""
        txtTrangthaidktcanhan.Text = ""
        txtMatrangthaidkttochuc.Text = ""
        txtTrangthaidkttochuc.Text = ""
        txtNgaynhantokhai.Text = ""
        txtLoaihinhkinhte.Text = ""
        txtTamnghitungay.Text = ""
        txtTamnghidenngay.Text = ""
        txtLydotamnghi.Text = ""
        txtLydodongmstcanhan.Text = ""
        txtNgaydongtrangthaicanhan.Text = ""
        txtLydodongmsttochuc.Text = ""
        txtNgaydongtrangthaitochuc.Text = ""
        txtQuoctich.Text = ""
        txtNgaysinh.Text = ""
        txtVonkinhdoanh.Text = ""
        txtNguonvonnsnn.Text = ""
        txtNguonvonnuocngoai.Text = ""
        txtNguonvonkhac.Text = ""
        txtNgaybatdaukinhdoanh.Text = ""
        txtDienthoaiban.Text = ""
        drpPage_Size.SelectedIndex = -1
        BindToGrid(1)
    End Sub
#End Region
End Class
